package com.freework.sys.dic.vo;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.freework.freedbm.base.BaseDTO;
import com.freework.freedbm.cfg.annotation.ReadDTOAnnotatin;
import com.freework.freedbm.cfg.annotation.TableCfg;
import com.freework.freedbm.cfg.tablecfg.TableDataManager;
import com.freework.freedbm.dao.jdbcm.JdbcForDTO;
import com.freework.freedbm.util.DTOByCfg;
import com.freework.util.unmodifiableMap.UnmodifiableKeyMap;

public  class PredefineVO implements BaseDTO{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2295890373299662641L;
	transient TableDataManager dataManager=null;
	protected MyUnmodifiableKeyMap map=null; 
	private transient HashSet<JdbcForDTO>  updateFieldIndexs=null;
	public PredefineVO(TableDataManager dataManager) {
		setTableDataManager(dataManager);
	}
	public PredefineVO(){
		setTableDataManager(DTOByCfg.getPredefineTableDataManager(this));
		
	}
	public void setTableDataManager (TableDataManager dataManager){
		this.dataManager = dataManager;
		map=new MyUnmodifiableKeyMap(((PredefineTableCfg)dataManager).getKeysIndex()); 
		updateFieldIndexs=new HashSet(dataManager.getFields().length);


	}
	protected void addUpdateField(String field){
		JdbcForDTO f=dataManager.getField(field);
		if(f!=null)
			updateFieldIndexs.add(f);
	}
	
	protected void addUpdateField(JdbcForDTO field){
		updateFieldIndexs.add(field);
	}


	
	public Object get(Object key) {
		return map.get(key);
	}
	public Object getIndex(int index) {
		return map.getIndex(index);
		
	}
	public boolean mapContainsKey(String key){
		return map.containsKey(key);
	}

	public Object put(String key, Object value) {
		
		return map.put(key, value,this);
	}
	
	public void putIndex(int index, Object value) {

		map.putIndex(index, value,this);
	}
	public Object remove(Object key) {
		return map.remove(key);
	}
	public int size() {
		return map.size();
	}
	
	
	
	public TableDataManager managerCfg() {
		return dataManager;
	}

	public JdbcForDTO[] updateField() {
		return updateFieldIndexs.toArray(new JdbcForDTO[updateFieldIndexs.size()]);
	}

	protected void addUpdateField(int index){
		PredefineTableCfg cfg=((PredefineTableCfg)dataManager);
		int i=index+cfg.getStartMapIndex();
		if(cfg.getFields().length<i)
			addUpdateField(cfg.getFields()[i]);
		
	}
	public String  toString(){
		JSONObject json=((JSONObject)JSON.toJSON(this));
		json.putAll(map);
		return json.toJSONString();
	}
	
}
